package com.pig.openg1.graphics;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.util.AttributeSet;
import android.util.Log;

import com.pig.openg1.data.Ball;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/***
 * 绘制球
 */
public class DrawView3  extends GLSurfaceView{

    private final String TAG = DrawView3.class.getSimpleName();

    private Ball mBall;

    public DrawView3(Context context) {
        super(context);
        init();
    }

    public DrawView3(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init()
    {
        setEGLContextClientVersion(2);
        setRenderer(new BallRender());
        setRenderMode(RENDERMODE_CONTINUOUSLY);
    }


    private class BallRender implements Renderer{


        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {

            //设置颜色
            GLES20.glClearColor(1,1,1,1);

            GLES20.glIsEnabled(GLES20.GL_DEPTH_TEST);

            mBall = new Ball(getContext());


        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {

            GLES20.glViewport(0,0,width,height);

            float ratio = (float)width/ height;
            //设置投影视图
            Matrix.frustumM(mBall.mProjMatrix,0,-ratio,ratio,-1,1,3,20);
            //设置相机试图
            Matrix.setLookAtM(mBall.mVMatrix,0,
                    0.0F,0.0F,15.0F,
                    0F,0.0F,0F,
                    0F,1.0F,0.0F);

            Matrix.multiplyMM(mBall.mMVPMatrix,0,mBall.mProjMatrix,0,
                    mBall.mVMatrix,0);
        }

        @Override
        public void onDrawFrame(GL10 gl) {

            GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT| GLES20.GL_COLOR_BUFFER_BIT);

            mBall.drawSelf();

        }


    }

}
